package com.kdl.webconfig.business;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.kdl.webconfig.entity.ButtonConfig;
import com.kdl.webconfig.entity.News;
import com.kdl.webconfig.entity.NewsDetail;
import com.kdl.webconfig.entity.PicConfig;
import com.kdl.webconfig.enums.PicTypeEnum;
import com.kdl.webconfig.service.IButtonConfigService;
import com.kdl.webconfig.service.INewsDetailService;
import com.kdl.webconfig.service.INewsService;
import com.kdl.webconfig.service.IPicConfigService;
import com.kdl.webconfig.vo.h5.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author 王成杰
 * @description TODO
 * @date 2022-09-09 下午6:40
 */
@Component
public class H5Business {


    @Autowired
    private IPicConfigService iPicConfigService;

    @Autowired
    private IButtonConfigService iButtonConfigService;

    @Autowired
    private INewsDetailService iNewsDetailService;

    @Autowired
    private INewsService iNewsService;


    /**
     * 获取首页信息
     * @param lanType
     * @return
     */
    public HomePageVO queryHomePageInfo(Integer lanType) {
        HomePageVO homePageVO = new HomePageVO();
        List<PicConfig> lbtPicConfigs = iPicConfigService.queryInfoByTypeAndLanType(PicTypeEnum.LBT.getCode(),lanType);
        List<H5PicConfigVO> lbtH5PicConfigVO = buildH5PicConfigVOS(lbtPicConfigs);
        homePageVO.setLbt(lbtH5PicConfigVO);
        return homePageVO;
    }


    private List<H5PicConfigVO> buildH5PicConfigVOS(List<PicConfig> picConfigs) {
        List<H5PicConfigVO> h5PicConfigVOList = Lists.newArrayList();
        for (PicConfig picConfig : picConfigs) {
            H5PicConfigVO h5PicConfigVO = buildH5PicConfigVO(picConfig);
            List<ButtonConfig> buttonConfigs = iButtonConfigService.selectByPicId(picConfig.getId());
            if(CollectionUtils.isNotEmpty(buttonConfigs)){
                List<H5ButtonConfigVO> h5ButtonConfigVOS = buildH5ButtonConfigVOS(buttonConfigs);
                h5PicConfigVO.setButtonConfigVOS(h5ButtonConfigVOS);
            }
            h5PicConfigVOList.add(h5PicConfigVO);
        }
        return h5PicConfigVOList;
    }

    private List<H5ButtonConfigVO> buildH5ButtonConfigVOS(List<ButtonConfig> buttonConfigs) {
        List<H5ButtonConfigVO> h5ButtonConfigVOS = Lists.newArrayList();
        for (ButtonConfig buttonConfig : buttonConfigs) {
            H5ButtonConfigVO h5ButtonConfigVO = new H5ButtonConfigVO();
            h5ButtonConfigVO.setComment(buttonConfig.getComment());
            h5ButtonConfigVO.setLinkUrl(buttonConfig.getLinkUrl());
            h5ButtonConfigVOS.add(h5ButtonConfigVO);
        }
        return h5ButtonConfigVOS;
    }

    private H5PicConfigVO buildH5PicConfigVO(PicConfig picConfig) {
        H5PicConfigVO h5PicConfigVO = new H5PicConfigVO();
        h5PicConfigVO.setId(picConfig.getId());
        h5PicConfigVO.setTitle(picConfig.getTitle());
        h5PicConfigVO.setComment(picConfig.getComment());
        h5PicConfigVO.setPicUrl(picConfig.getPicUrl());
        return h5PicConfigVO;
    }


    /**
     * h5获取News页面信息
     * @param lanType
     * @return
     */
    public NewsPageVO queryNewsPageInfo(Integer lanType) {
        NewsPageVO newsPageVO = new NewsPageVO();
        List<PicConfig> configList = iPicConfigService.queryInfoByTypeAndLanType(PicTypeEnum.NEWS_BANNER.getCode(),lanType);
        List<H5PicConfigVO> h5PicConfigVOList = buildH5PicConfigVOS(configList);
        newsPageVO.setBannerPicConfigVOS(h5PicConfigVOList);
        List<News> newsList = iNewsService.selectListByLanType(lanType);
        List<H5NewsVO> h5NewsVOS = buildH5NewsVOS(newsList);
        newsPageVO.setH5NewsVOS(h5NewsVOS);
        return newsPageVO;
    }

    private List<H5NewsVO> buildH5NewsVOS(List<News> newsList) {
        List<H5NewsVO> h5NewsVOS = Lists.newArrayList();
        for (News news : newsList) {
            H5NewsVO h5NewsVO = new H5NewsVO();
            h5NewsVO.setId(news.getId());
            h5NewsVO.setComment(news.getComment());
            h5NewsVO.setLinkUrl(news.getLinkUrl());
            h5NewsVO.setPicUrl(news.getPicUrl());
            h5NewsVO.setTitle(news.getTitle());
            h5NewsVOS.add(h5NewsVO);
        }
        return h5NewsVOS;
    }

    /**
     * h5获取News详情信息
     * @param newsId
     * @return
     */
    public H5NewsDetailVO queryNewsDetailByNewId(Integer newsId) {
        NewsDetail newsDetail = iNewsDetailService.selectOneByNewsId(newsId);
        return buildDetailVO(newsDetail);
    }

    private H5NewsDetailVO buildDetailVO(NewsDetail newsDetail) {
        if(Objects.isNull(newsDetail)){
            return null;
        }
        H5NewsDetailVO h5NewsDetailVO = new H5NewsDetailVO();
        h5NewsDetailVO.setDate(newsDetail.getDate());
        h5NewsDetailVO.setHtmlContent(newsDetail.getHtmlContent());
        h5NewsDetailVO.setTitle(newsDetail.getTitle());
        return h5NewsDetailVO;
    }
}
